from sqlalchemy import  Column,create_engine,String,Integer
from sqlalchemy.orm import  sessionmaker
from sqlalchemy.ext.declarative import  declarative_base
# 创建父类
Base=declarative_base()
# 子类继承declarative_base类
class math(Base):
    __tablename__="math"
    id=Column(String(20),primary_key=True)
    name=Column(String(50))
    ms=Column(Integer)  #ms,math score的缩写，表示数学成绩
# 创建连接create_engine+DBSession
engine=create_engine("mysql+mysqlconnector://root:132786@localhost:3306/score")

DBSession=sessionmaker(bind=engine)
session=DBSession()

# 创建对象
lilizong=math(id="0001",name="李立宗",ms=100)
zhaosi=math(id="888",name="四哥",ms=99)
xieguangkun=math(id="666",name="广坤",ms=66)
# 插入数据库
session.add(lilizong)
session.add(zhaosi)
session.add(xieguangkun)
# 提交
session.commit()
# 关闭连接
session.close()


















# # 导入SQLAlchemy模块
# from sqlalchemy import Column, create_engine,String,Integer
# from sqlalchemy.orm import sessionmaker
# from sqlalchemy.ext.declarative import declarative_base
# # 创建对象的基类:
# Base = declarative_base()
# # 定义math:
# class math(Base):
#     # 表名称：
#     __tablename__ = 'math'
#     # 字段:
#     id = Column(String(20), primary_key=True)
#     name = Column(String(50))
#     ms = Column(Integer)
# # 初始化数据库连接:
# engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/score')
# # 创建DBSession类型:
# DBSession = sessionmaker(bind=engine)
# # 创建session对象:
# session = DBSession()
# # 创建对象:
# lilizong = math(id='001', name='李立宗', ms=88)
# zhaosi = math(id='002', name='赵四', ms=99)
# xieguangkun  = math(id='003', name='谢广坤', ms=66)
# # 添加到session:
# session.add(lilizong)
# session.add(zhaosi)
# session.add(xieguangkun)
# # 提交，即保存到数据库:
# session.commit()
# # 关闭session:
# session.close()
